﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomeService.Infrastructure.Filters;
using HomeService.Models;

namespace HomeService.Controllers
{
    public class ServiceRequestController : Controller
    {
        public ViewResult AddBasicInfo(ServiceRequest request)
        {
            return View(request.BasicInfo);
        }

        [HttpPost]
        public ActionResult AddBasicInfoAndContinue(ServiceRequest request, ServiceRequestBasicInfo basicInfo)
        {
            if (ModelState.IsValid) {
                request.BasicInfo = basicInfo;
                return RedirectToAction("AddPhotos");
            }
            return View("AddBasicInfo");
        }

        [ImportModelStateFromTempData]
        public ViewResult AddPhotos(ServiceRequest request)
        {
            return View(request);
        }
        
        [HttpPost]
        public ActionResult AddPhotosAndContinue(ServiceRequest request)
        {
            try {
                request.ValidatePhotos();
            } catch (ValidationException ex) {
                ex.CopyTo(ModelState);
            }
            if (ModelState.IsValid)
                return RedirectToAction("Confirm");
            return View("AddPhotos", request);
        }

        public ViewResult Confirm(ServiceRequest request)
        {
            return View(request);
        }

        public ViewResult Success()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SendRequest(ServiceRequest request)
        {
            try {
                request.Send();
            } catch (ValidationException ex) {
                ex.CopyTo(ModelState);
            }
            if (ModelState.IsValid)
                return RedirectToAction("Success");
            return View("Confirm", request);
        }

        #region Photo Actions

        [HttpPost, ExportModelStateToTempData]
        public ActionResult AddPhoto(ServiceRequest request, HttpPostedFileBase image, string description)
        {
            try {
                request.Photos.Add(new ServiceRequestPhoto(image, description));
            } catch (ValidationException ex) {
                ex.CopyTo(ModelState);
            }
            return RedirectToAction("AddPhotos");
        }

        [HttpPost, ExportModelStateToTempData]
        public ActionResult UpdatePhotoDescription(ServiceRequest request, string id, string description)
        {
            try {
                var photo = request.Photos.Where(x => x.Id == id).FirstOrDefault();
                if (photo != null)
                    photo.Description = description;
            } catch (ValidationException ex) {
                ex.CopyTo(ModelState);
            }
            return RedirectToAction("AddPhotos");
        }

        [HttpPost]
        public ActionResult RemovePhoto(ServiceRequest request, string id)
        {
            var photoToRemove = request.Photos.Where(x => x.Id == id).FirstOrDefault();
            if (photoToRemove != null)
                request.Photos.Remove(photoToRemove);
            return RedirectToAction("AddPhotos");
        }

        public FileContentResult GetPhotoImage(ServiceRequest request, string id)
        {
            var photo = request.Photos.Where(x => x.Id == id).FirstOrDefault();
            return File(photo.Bytes, photo.MimeType);
        }

        #endregion
    }
}
